#include "mythP2PDecoder.hh"
#include <memory.h>
mythP2PDecoder::mythP2PDecoder(int userid,const char* filename)
	:mythVirtualDecoder()
{
	_filename = filename;
	_userid = userid;
	_buffer = new char[1024 * 1024];
	_bufferindex = 0;
}


void mythP2PDecoder::OnRecv(void* src, int len)
{
	memcpy(_buffer + _bufferindex, src, len);
	_bufferindex += len;
	if (len < 4096){
		put((unsigned char*)_buffer, _bufferindex);
		_bufferindex = 0;
	}
}

void mythP2PDecoder::OnConnection(std::string ip, int port, std::string rtmplink, int UdpSock)
{

}

std::string mythP2PDecoder::onLink()
{
	return _filename;
}


int mythP2PDecoder::MainLoop()
{
	_server = VirtualP2PServer::CreateNew();
	_server->SetOnLink(onLinkStatic, this);
	_server->SetOnConnection(OnConnectionStatic, this);
	_server->SetOnRecvData(OnRecvStatic, this);
	_server->setUserid(_userid);
	_server->Start(&flag);
	return 0;
}

mythP2PDecoder::~mythP2PDecoder()
{
	delete [] _buffer;
}
